<template>
    <div class="interests">
        <el-dialog
            title="发放权益"
            :visible.sync="dialogVisible"
            width="30%"
            class='record'
            :close-on-click-modal='false'
            :before-close="handleClose">
            <div class='selectInput'>
                <el-select @change='changeValue' v-model="value" placeholder="请选择">
                    <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
                <el-input v-model="form.earnings" :placeholder="placeholder"></el-input>
            </div>
            <p 
                v-if="value === '每元盈利' && form.earnings && this.vails()">
                每1元赚{{form.earnings}}元，用户购买1元需返{{getProfitMoney()}}元给用户
            </p>
            <p 
                v-if="value === '每元盈亏' && form.earnings && this.vails()">
                每1元亏{{form.earnings}}元，用户购买1元需返{{getMoney()}}元给用户
            </p>
            <span slot="footer" class="dialog-footer">
                <el-button  @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="sureInterest()">确 定</el-button>
            </span> 
        </el-dialog>  
    </div>
</template>

<script>
import { ajax } from "@/api";
import { getToken } from "@/utils/auth";
export default {
    name:'interests',
    data(){
        return {
            placeholder:'请输入每元盈利值，需大于等于0',
            dialogVisible:false,
            form:{
                earnings:'',
                profit:''
            },
            value:'每元盈利',
            options:[{
                label:'每元盈利',
                value:'每元盈利'
            },{
                label:'每元盈亏',
                value:'每元盈亏'
            }],
            params:{
                movieId:'',
                profit:'',
                token:getToken()
            }
        }
    },
    methods:{
        changeValue(){
            if(this.value === '每元盈利'){
                this.placeholder = '请输入每元盈利值，需大于等于0';
            }else{
                this.placeholder = '请输入每元盈亏值，0~1之间';
            }

        },
        opendialogVisible(item){
            this.params.movieId = item.movieId;
            this.dialogVisible = true;
        },
        //影片权益发放
        sureInterest(){
            if(this.vails()){
                this.params.profit = parseFloat(this.form.earnings);    
                if(this.value === '每元盈亏'){
                    this.params.profit = -(parseFloat(this.form.earnings));  
                }
                
                ajax('movieProfit',this.params).then((res) => {
                        if(res.data.status){
                            this.$message.success('权益发放提交成功，请等待系统审核！');
                            this.dialogVisible = false;
                            setTimeout(() => {
                                location.reload();
                            },100);
                            

                        }else{
                            this.$message.error(res.data.message);
                        }
                });
            }
        },
        vails(){
            if(this.value === '每元盈亏'){
                if(this.form.earnings > 1 || this.form.earnings < 0){
                    this.$message.error('盈亏输入的数字只能为0到1之间');
                    return false;
                }else{
                    return true;
                }
            }else{
                if(this.form.earnings < 0){
                    this.$message.error('盈利输入的数字需大于等于0');
                    return false;
                }else{
                    return true;
                }
            }
            
        },
        getMoney(){
            if(!isNaN(this.form.earnings)){
                return 1-parseFloat(this.form.earnings);
            }else{
                return 0
            }
        },

        getProfitMoney(){
            if(!isNaN(this.form.earnings)){
                return 1+parseFloat(this.form.earnings);
            }else{
                return 0
            }
        },
        
        handleClose(done){
            done();
            this.params.movieId = '';
        }
    }
}
</script>


<style lang="less" scoped>
.interests{
    .selectInput{
        display: flex;
        .el-select{
            margin-right: 1rem;
        }
    }
}
</style>
